package Cliente.ventanas;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import Cliente.TCPCliente;

public class SensorWindow extends JFrame implements ItemListener, MouseListener, ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JComboBox sensoresList;
	private JButton histList;
	private JButton activar;
	private JButton volver;
	private JLayeredPane layer;
	private JLabel valorLabel;
	private JLabel idLabel;
	private JTextField descField;
	private JTextField valorField;
	private JTextArea idField;
	private JRadioButton estado;
	TCPCliente tcp;
	private ArrayList<String> lista;
	
	public SensorWindow(int vehiculo, TCPCliente tcp){
		
		estado = new JRadioButton("OFF");
		estado.setForeground(Color.white);
		estado.addMouseListener(this);
		lista = new ArrayList<String>(); 
		this.tcp = tcp;
		LISTSENSOR();
		obtenerLineas(lista);
		this.setResizable(false);
		volver = new JButton("Volver");
		activar = new JButton("Activar");
		idLabel = new JLabel("Id :");
		idField = new JTextArea();
		idLabel.setForeground(Color.WHITE);
		histList = new JButton("Historico ");
		valorLabel = new JLabel("Valor :");
		valorField = new JTextField();
		valorLabel.setForeground(Color.white);
		
		descField = new JTextField();
		
		JLabel fondo = new JLabel(new ImageIcon("src/Img/fondopeq.jpg"));
		layer = new JLayeredPane();
		
		actualizarDatos(lista.get(0));
		
		ArrayList<String>listaSensores= new ArrayList<String>();
		for(String st:lista){
			int index=st.indexOf(';');
			listaSensores.add("Sensor numero: "+st.substring(0, index));
		}

		sensoresList = new JComboBox(listaSensores.toArray());
		sensoresList.addItemListener(this);
		volver.addActionListener(this);
		
		this.add(layer);		
		layer.setBounds(0, 0, 500, 300);
		
		layer.add(fondo, new Integer(0));
		layer.add(sensoresList, new Integer(1));
		layer.add(volver, new Integer(1));
		layer.add(descField, new Integer(1));
		layer.add(valorField, new Integer (1));
		layer.add(valorLabel, new Integer (1));
		layer.add(idLabel, new Integer(1));
		layer.add(idField, new Integer(1));
		layer.add(estado,new Integer(1));
		layer.add(activar, new Integer(1));
		
		layer.add(histList, new Integer(1));
				
		fondo.setBounds(0, 0, 500, 300);
		sensoresList.setBounds(20, 20, 150, 50);

		estado.setBounds(350,170,200,50);
		estado.setOpaque(false);
		
		volver.setBounds(180,215,100,30);
		histList.setBounds(60, 215, 100, 30);
		valorLabel.setBounds(20, 150, 150, 30);
		valorField.setBounds(60, 160, 150, 20);
		idLabel.setBounds(20, 80, 150, 50);
		idField.setBounds(60, 95, 150, 20);
		descField.setBounds(300,20,150,125);
		activar.setBounds(230, 160, 100, 20);
		
		activar.addActionListener(this);
		histList.addActionListener(this);
		
		this.setSize(500,300);
		this.setVisible(true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		
	}
	
	
	public  void obtenerLineas(ArrayList<String> array){
		boolean fin =false;
		try {
			while(!fin){
				String linea = tcp.receiveMessage();
				array.add(linea);
				if(linea.equals("222 OK ListaFinalizada."))
					fin = true;
			}
			array.remove(0);
			array.remove(0);
			array.remove(array.size()-1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void LISTSENSOR() {
		try {
			tcp.sentMessage("LISTSENSOR\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void ON (int id){
		try {
			tcp.sentMessage("ON " + id + "\n");
			tcp.receiveMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void OFF (int id){
		try {
			tcp.sentMessage("OFF " + id + "\n");
			tcp.receiveMessage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void GET_VALACT (int id){
		try {
			tcp.sentMessage("GET_VALACT " + id + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		String linea = lista.get(sensoresList.getSelectedIndex());
		actualizarDatos(linea);
//		descField.setText(linea.substring(finID,finDesc));
//		for(int inicio = finID; ch!=';' && inicio<linea.length();inicio++){
//			ch = linea.charAt(inicio);
//			finID = inicio;
//		}
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(estado.isSelected()){
			ON(Integer.parseInt(idField.getText()));
			estado.setText("OFF");
		}
		else{
				OFF(Integer.parseInt(idField.getText()));
				estado.setText("ON");
		}
	
	}

	public void actualizarDatos(String linea){
		char ch = '0';
		int finID = 0 ;
		int finDesc = 0;
		
		for(int inicio = 0; ch!=';' && inicio<linea.length();inicio++){
			ch = linea.charAt(inicio);
			finID = inicio;
		}
		idField.setText(linea.substring(0,finID));
		ch='0';
		for(int inicio = finID+1; ch!=';' && inicio<linea.length();inicio++){
			ch = linea.charAt(inicio);
			finDesc = inicio;
		}
		descField.setText(linea.substring(finID+1, finDesc));
		if(linea.substring(finDesc+1, linea.length()).equals("ON")){
			estado.setSelected(true);
			estado.setText("OFF");
	}
		else{
			estado.setSelected(false);
			estado.setText("ON");
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==activar){
			GET_VALACT(Integer.parseInt(idField.getText()));
			try {
				String recibido=tcp.receiveMessage();
				int index=recibido.lastIndexOf(';');
				valorField.setText(recibido.substring(index+1,recibido.length()));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(e.getSource()==histList){
			new HistoricoWindow(this,tcp,Integer.parseInt(idField.getText()));
		}
		if(e.getSource()==volver){
			this.dispose();
		}
	}
}
